package blt.app_core.api.login;


import android.util.Log;

import blt.app_core.api.base.IBaseApi;
import blt.app_core.api.base.bean.OAuth;
import blt.app_core.api.base.bean.State;
import blt.app_core.api.base.callback.StateCallback;
import blt.app_core.api.impl.BaseImpl;
import blt.app_core.api.login.bean.Cookie;
import blt.app_core.api.login.bean.User;
import blt.app_core.log.Logger;
import blt.app_core.utils.CacheUtil;
import blt.app_core.utils.CommonPref;
import blt.app_core.utils.PreferencesConstant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * auther xgp
 * on 2017/6/12.
 */

public class LoginImpl extends BaseImpl<LoginService> implements LoginApi {


    @Override
    public void login(final String user, String password) {
        mService.login(user, password).enqueue(new StateCallback<Cookie>() {
            @Override
            protected void onSuccess(Cookie data) {
                String cookie = data.userCookieName;
                String userId = data.tbUser.id;
                CacheUtil.getCache().saveCookie(cookie);
                CacheUtil.getCache().saveUid(userId);
                //登陆完成之后重新builder request
                builderLoginRequest();
                notifyClients(LoginApi.class, LoginApi.ON_LOGIN);
            }
        });
//        mService.login(user, password).enqueue(new StateCallback() {
//            @Override
//            public void onSuccess(State body) {
//
//
//            }
//        });
    }

    @Override
    public void logout() {
        mService.logOut().enqueue(new Callback<State>() {
            @Override
            public void onResponse(Call<State> call, Response<State> response) {
                notifyClients(LoginApi.class, LoginApi.onLogout);

            }

            @Override
            public void onFailure(Call<State> call, Throwable t) {

            }
        });
        CacheUtil.getCache().clear();
        CommonPref.getInstance().removeForKey(PreferencesConstant.REMEMBER_PASSWORD);
    }

    @Override
    public boolean isLogin() {
        return CacheUtil.getCache().getCookie() != null;
    }

    @Override
    public void modifyPwd(String phone, String pwd) {
        mService.modifyPwd(phone, pwd).enqueue(new StateCallback<Cookie>() {
            @Override
            protected void onSuccess(Cookie data) {
                updateUserInfo(data);
                notifyClients(LoginApi.class, LoginApi.onModify);
            }
        });
    }

    /**
     * 更新用户信息
     *
     * @param data
     */
    private void updateUserInfo(Cookie data) {
        CommonPref.getInstance().saveString(PreferencesConstant.PASSWORD, data.tbUser.password);
        CacheUtil.getCache().saveCookie(data.userCookieName);
    }


    @Override
    public void forgetPassword(String username, String password, String verCode) {
        mService.forgetPwd(username, password, verCode).enqueue(new StateCallback<Cookie>() {
            @Override
            protected void onSuccess(Cookie data) {
                notifyClients(LoginApi.class, LoginApi.REGISTER, state.errmsg);
            }
        });

    }

    @Override
    public String getVerCode(String phone) {
        mService.getVerCode(phone).enqueue(new Callback<State>() {
            @Override
            public void onResponse(Call<State> call, retrofit2.Response<State> response) {
                notifyClients(LoginApi.class, LoginApi.TEST);
            }

            @Override
            public void onFailure(Call<State> call, Throwable t) {
                notifyClients(IBaseApi.class, IBaseApi.ERROR_MSG, "获取验证码失败");
            }
        });

//        mService.getVerCode(phone).enqueue(new Callback<State>() {
//            @Override
//            public void onResponse(Call<State> call, retrofit2.Response<State> response) {
//                if (response.isSuccessful()) {
//                    Logger.d("onResponse:" + response.body());
////                    notifyClients(LoginApi.class, LoginApi.TEST, response.body());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<State> call, Throwable t) {
//                Logger.d("onFailure:" + t.toString());
//            }
//        });
        return null;
    }

    @Override
    public void register(String verCode, String phone, String password) {
        mService.register(phone, verCode, password).enqueue(new StateCallback<Cookie>() {
            @Override
            protected void onSuccess(Cookie data) {
                String cookie = data.userCookieName;
                String userId = data.tbUser.id;
                CacheUtil.getCache().saveCookie(cookie);
                CacheUtil.getCache().saveUid(userId);
                notifyClients(LoginApi.class, LoginApi.REGISTER, state.errmsg);

            }
        });
//        mService.register(phone, verCode, password).enqueue(new StateCallback() {
//            @Override
//            public void onSuccess(State body) {
////                cookie = body.data.userCookieName;
//            }
//        });

    }

    @Override
    public void modifyNick(String nick) {
        mService.modifyNick(nick).enqueue(new StateCallback<String>() {
            @Override
            protected void onSuccess(String data) {
                notifyClients(LoginApi.class, LoginApi.onModifyNick);
            }
        });
    }

    @Override
    public void getUserInfo() {
        builderLoginRequest();
        mService.getUserInfo().enqueue(new StateCallback<User>() {
            @Override
            protected void onSuccess(User data) {
                notifyClients(LoginApi.class, LoginApi.onUserInfo, data);

            }
        });

    }


//    @Override
//    public void upload(String path) {
//        Map<String, String> params = new HashMap<>();
//
//        Http.upload(UriProdiver.BASE_URL+UriProdiver.UPlOAD_ICON, path, params, new HttpCallback() {
//            @Override
//            public void onFailure(Request request, Response response, Exception e) {
//                Logger.d("onFailure code" + e.toString());
//            }
//
//            @Override
//            public void onSuccess(Object response, int code) {
//                Logger.d("onSuccess code" + code + response.toString());
//            }
//
//        });
//    }


}
